﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using AMatysiak.Methods;
using System.Reflection;

namespace AMatysiak.Controls
{
    public partial class YearSelector : UserControl
    {
        [Browsable(true)]
        public event EventHandler Year_ValueChanged;

        public YearSelector()
        {
            InitializeComponent();

            setYearsValues();
        }

        private void setYearsValues()
        {
            MinYear = 1900;
            MaxYear = 2100;
            setYear(DateTime.Now.Year);
        }

        [Description("Minimum year which can be reached"),
        Category("Values"),
        DefaultValue(1900),
        Browsable(true)]
        public int MinYear { get; set; }

        [Description("Maximum year which can be reached"),
        Category("Values"),
        DefaultValue(2100),
        Browsable(true)]
        public int MaxYear { get; set; }

        [Description("DefaultYear"),
        Category("Values"),
        DefaultValue(2010),
        Browsable(true)]
        public int Year
        {
            get
            {
                return (int)yearBox.Tag;
            }
            set
            {
                if (validateFailed(value)) return;

                setYear(value);

                if(Year_ValueChanged != null)
                    Year_ValueChanged(this, new EventArgs());
            }
        }

        private bool validateFailed(int value)
        {
            return !validate(value);
        }

        private void setYear(int value)
        {
            yearBox.Tag = value;
            yearBox.Text = value.ToString();
        }

        private void yearBox_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (validate(tb.Text))
                Year = tb.Text.ToInt();
        }

        private bool validate(string text)
        {
            if (text == "") return false;

            int year;
            if (!int.TryParse(text, out year)) return false;

            return validate(year);
        }

        private bool validate(int year)
        {
            if (valueIsNotInTimeInterval(year)) return false;

            return true;
        }

        private bool valueIsNotInTimeInterval(int year)
        {
            return year < MinYear || year > MaxYear;
        }

        protected void yearBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = (TextBox)sender;

            if (valueIsNotValid(tb))
            {
                e.Cancel = true;
                MessageBox.Show("Musi to być wartość liczbowa z przedziału {0}-{1}".FormatWith(MinYear, MaxYear), 
                    "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool valueIsNotValid(TextBox tb)
        {
            return !validate(tb.Text);
        }

        private void mover_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int move = (int)btn.Tag;
            Year += move;
        }
    }
}
